﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Reflection;
using Pixysoft.Weblications.Sango.Dao;
using Pixysoft.Weblications.Sango.Formular;
using Pixysoft.Weblications.Sango.Environment;
using Pixysoft.Weblications.Sango.Constant;
using Pixysoft.Weblications.Sango.Protocol.party;
using Pixysoft.Weblications.Sango.Protocol;

namespace Pixysoft.Weblications.Sango.Command.party
{
    partial class SangoPartyHandler
    {
        public party_promote party_promote()
        {
            string userCode = SangoManager.Authentication();

            party_promote response = ReflectionManager.CreatePojo<party_promote>();
            response.Succeed = false;

            Entities.Usr_Profile profile = UsrProfileDao.getUsrProfile(userCode);

            Entities.Usr_Partymember upgradeMember = PartyDao.tryGetActiveMember(userCode);

            if (upgradeMember == null)
            {
                response.Message = "尚未加入军团";
                return response;
            }

            if (upgradeMember.Titletypecode == PtyTitleSerialcodeCollection.POSITION_PRESIDENT)
            {
                response.Message = "无法继续升级";
                return response;
            }

            Entities.Usr_Party party = PartyDao.tryGetActiveParty(upgradeMember.Partyprimarycode);

            int ranking = PartyDao.getMemberRanking(upgradeMember.Partyprimarycode, upgradeMember.Partycontributionvalue);

            long titletypecode = upgradeMember.Titletypecode + 1;

            int totalmembers = PartyDao.getTotalActiveMembers(upgradeMember.Partyprimarycode);

            Entities.Pty_Partytitle position = PtyPartyTitleHelper.searchByCode(titletypecode);

            if (!PartyFormular.canMemberUpgrade(position, totalmembers, ranking, upgradeMember.Partycontributionvalue))
            {
                response.Message = "无法升级官职";
                return response;
            }

            // 删除新阶层

            // 2011-01-17 检查是否需要排挤原有会员

            int currentmembers = PartyDao.getTotalActiveMembers(upgradeMember.Partyprimarycode, titletypecode);

            IEntityNoebeTransaction command = SettingHelper.EntityTransaction;

            command.Begin(delegate()
            {
                if (PartyFormular.needMemberDowngrade(totalmembers, currentmembers, titletypecode))
                {
                    Entities.Usr_Partymember downgradeMember = PartyDao.getLastActiveMembersByTitle(upgradeMember.Partyprimarycode, titletypecode);

                    if (downgradeMember != null)
                    {
                        Entities.Usr_Profile downgradeProfile = UsrProfileDao.getUsrProfile(downgradeMember.Usercode);
                        PartyDao.memberDowngrade(downgradeMember, downgradeProfile);
                        command.Update(downgradeProfile);
                        command.Update(downgradeMember);
                    }
                }

                PartyDao.memberUpgrade(upgradeMember, profile);

                command.Update(upgradeMember);

                command.Update(profile);

                command.Commit();
            });

            int requireRanking = -1;

            if (upgradeMember.Titletypecode >= PtyTitleSerialcodeCollection.POSITION_VICE_PRESIDENT)
            {
                requireRanking = -1;
            }
            else
            {
                position = PtyPartyTitleHelper.searchByCode(titletypecode);
                requireRanking = (int)PartyFormular.getUpgradeRequiredRanking(position, totalmembers);
            }

            response.rank = ranking;
            response.member = PartyFacade.ToPartyMember(profile, party, upgradeMember);
            response.upgraderank = requireRanking;
            response.Succeed = true;
            return response;
        }


    }
    public interface party_promote : ISangoResponse
    {
        party_member member { get;set;}
        long rank { get;set;}
        long upgraderank { get;set;}
    }
}
